<template>
  <page-header-wrapper :title="false">
    <a-card title="商品提成详情">
      <template #extra>
        <a-tooltip>
          <template #title> 数据导出</template>
          <a-button type="primary" icon="database" @click="handleExpert(queryParam)"></a-button>
        </a-tooltip>
      </template>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.queryCondition" allow-clear placeholder="请输入" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" :loading="loading" @click="handleSearch(queryParam)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-table
        ref="table"
        size="default"
        rowKey="key"
        :scroll="{ x: 1920 }"
        :pagination="pagination"
        @change="handleTableChange"
        :columns="columns"
        :data-source="loadData"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="saleType" slot-scope="text, record">
          <div v-if="record.saleType == 0">售出</div>
          <div v-if="record.saleType == 1">售后</div>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a-tag color="#428bca" @click="handle(record)">重算提成</a-tag>
            <a-tag color="#428bca" @click="handleDetail(record)">进入</a-tag>
          </template>
        </span>
      </a-table>
      <comm-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>
<script>
import { getEmpSalesDetailListUsingPOST ,exportEmpSalesDetailListUsingPOST} from  '@/api/api/qiantaizhangwu.ts'
import CommForm from './commForm'
export default {
  name: 'OrderGoods',
  components: {
    CommForm,
  },
  data() {
    return {
      mdl: null,
      visible: false,
      value: '',
      loading: false,
      confirmLoading: false,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        pageNo:1,
        pageSize:10,
        queryCondition:'',
        status:''
      },
      pagination:{
        total: 0
      },
      // 表头
      columns: [
        {
          title: '订单ID',
          dataIndex: 'orderId',
          width: 180,
        },
        {
          title: '销售日期',
          dataIndex: 'orderDate',
          width: 160,
        },
        {
          title: '类型',
          dataIndex: 'saleType',
          scopedSlots: { customRender: 'saleType' },
          width: 100,
        },
        {
          title: '商品名',
          dataIndex: 'goodsName',
          width: 120,
        },
        {
          title: '品牌',
          dataIndex: 'brandName',
          width: 100,
        },
        {
          title: '一级分类',
          dataIndex: 'topName',
          width: 100,
        },
        {
          title: '二级分类',
          dataIndex: 'subName',
          width: 100,
        },
        {
          title: '串码1',
          dataIndex: 'phoneCode1',
          width: 144,
        },
        {
          title: '串码2',
          dataIndex: 'phoneCode2',
          width: 144,
        },
        {
          title: '成本(元)',
          dataIndex: 'cost',
          width: 100,
        },
        {
          title: '赠品成本(元)',
          dataIndex: 'giftCost',
          width: 120,
        },
        {
          title: '售价(元)',
          dataIndex: 'price',
          width: 100,
        },
        {
          title: '毛利(元)',
          dataIndex: 'grossProfit',
          width: 100,
        },
        {
          title: '考核成本(元)',
          dataIndex: 'assCost',
          width: 120,
        },
        {
          title: '赠品考核成本(元)',
          dataIndex: 'giftAssCost',
          width: 144,
        },
        {
          title: '考核毛利(元)',
          dataIndex: 'assGrossProfit',
          width: 120,
        },
        {
          title: '提成比例',
          dataIndex: 'propWage',
          width: 100,
        },
        {
          title: '固定提成(元)',
          dataIndex: 'guding',
          width: 120,
        },
        {
          title: '提成(元)',
          dataIndex: 'amount',
          width: 100,
        },
        {
          title: '销售员',
          dataIndex: 'empName',
          width: 100,
        },
        {
          title: '销售店面',
          dataIndex: 'storeName',
          width: 120,
        },
        {
          title: '会员名',
          dataIndex: 'memName',
          width: 144,
        },
        {
          title: '会员电话',
          dataIndex: 'memPhone',
          width: 120,
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 160,
          fixed: 'right',
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: [],
      optionAlertShow: false,
    }
  },
  mounted(){
    this.handleSearch(this.queryParam)
  },
  methods: {
    handleExpert(params){
      exportEmpSalesDetailListUsingPOST({ ...params,empId:this.$route.params.id}).then((res) => {
          const blob = new Blob([res], {
            type: 'application/vnd.ms-excel;charset=utf-8',
          })
          const filename = '商品提成详情' + '.xlsx'
          // 将blob对象转为一个URL
          var blobURL = window.URL.createObjectURL(blob)
          // 创建一个a标签
          var tempLink = document.createElement('a')
          // 隐藏a标签
          tempLink.style.display = 'none'
          // 设置a标签的href属性为blob对象转化的URL
          tempLink.href = blobURL
          // 给a标签添加下载属性
          tempLink.setAttribute('download', filename)
          if (typeof tempLink.download === 'undefined') {
            tempLink.setAttribute('target', '_blank')
          }
          // 将a标签添加到body当中
          document.body.appendChild(tempLink)
          // 启动下载
          tempLink.click()
          // 下载完毕删除a标签
          document.body.removeChild(tempLink)
          window.URL.revokeObjectURL(blobURL)
          this.$message.success('导出成功~')
        })
    },
    handleSearch (params) {
      this.loading = true
      getEmpSalesDetailListUsingPOST({
        ...params,
        empId:this.$route.params.id ,
        startTime:`${this.$route.query.wageMonth.substr(0,4)}-${this.$route.query.wageMonth.substr(4,2)}-01`,
        endTime:`${this.$route.query.wageMonth.substr(0,4)}-${this.$route.query.wageMonth.substr(4,2)}-31`
      }).then((res) => {
        this.loadData = res.data.list
        this.loading = false
        this.queryParam.pageNo = this.pagination.current
        this.pagination = { ...this.pagination, total: Number(res.data.total) }
      })
      .catch(() => {
        this.loading = false
        this.$message.error('获取列表失败,请联系管理员')
      })
    },
    handleTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
      pager.pageSize = pagination.pageSize
      this.pagination = pager
      this.handleSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo:pagination.current,
        pageSize:pagination.pageSize,
        queryCondition:this.queryParam.queryCondition,
        status:this.queryParam.status,
        date:this.queryParam.date
      })
    },
    handleDetail(record) {
      if(record.saleType == 0){
        this.$router.push({ path: `/cashier/profile/index/${record?.orderId}` })
      }else{
        this.$router.push({ path: `/service/service/detail/${record.id}` })
      }
    },
    handle(record) {
      this.visible = true
      this.mdl = record
      this.confirmLoading = false
    },
    handleOk() {
      this.confirmLoading = true
      setTimeout(() => {
        this.confirmLoading = false
        this.visible = false
      }, 1000)
      this.$message.warn('接口开发中')
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
  },
}
</script>
